package cn.edu.zut.score.service.impl;

import cn.edu.zut.score.core.cache.RedisCache;
import cn.edu.zut.score.core.constant.RoleEnum;
import cn.edu.zut.score.core.convert.ClazzConvert;
import cn.edu.zut.score.core.domain.PageResponse;
import cn.edu.zut.score.core.domain.RestResponse;
import cn.edu.zut.score.core.dto.request.ClazzPageRequest;
import cn.edu.zut.score.core.dto.request.UserPageRequest;
import cn.edu.zut.score.core.dto.response.ClazzResponse;
import cn.edu.zut.score.core.dto.response.UserResponse;
import cn.edu.zut.score.core.entity.Clazz;
import cn.edu.zut.score.core.entity.Info;
import cn.edu.zut.score.core.util.StringUtils;
import cn.edu.zut.score.mapper.ClazzMapper;
import cn.edu.zut.score.service.IClazzService;
import cn.edu.zut.score.service.IUserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zby
 * @since 2021-06-25
 */
@Slf4j
@Service
public class ClazzServiceImpl extends ServiceImpl<ClazzMapper, Clazz> implements IClazzService {

    @Autowired
    private HttpServletRequest httpServletRequest;

    @Autowired
    private RedisCache redisCache;

    @Autowired(required = false)
    private ClazzMapper clazzMapper;

    @Autowired
    private IUserService iUserService;

    @Override
    public RestResponse<ClazzResponse> saveClazz(Clazz clazz) {
        if (clazz.getId() == null) {
            clazz.fillCreateInfo(httpServletRequest, redisCache);
        } else {
            clazz.fillUpdateInfo(httpServletRequest, redisCache);
        }
        this.saveOrUpdate(clazz);
        return RestResponse.success(ClazzConvert.INSTANCE.clazz2Response(clazz));
    }

    @Override
    public RestResponse<Long> deleteClassById(Long classid) {
        this.removeById(classid);
        log.info("成功删除班级{}", classid);
        return RestResponse.success(classid);
    }

    @Override
    public RestResponse<Integer> deleteClassByIds(List<Long> ids) {
        Integer sum = 0;
        for (Long id : ids) {
            boolean deleted = this.removeById(id);
            if (deleted) {
                sum++;
                log.info("成功删除班级{}", id);
            }
        }
        return RestResponse.success(sum);
    }

    @Override
    public RestResponse<ClazzResponse> queryClass(Long classid) {
        Clazz clazz = this.getById(classid);
        return RestResponse.success(ClazzConvert.INSTANCE.clazz2Response(clazz));
    }

    @Override
    public RestResponse<List<Info>> queryAllClasses() {
        LambdaQueryWrapper<Clazz> qw = new LambdaQueryWrapper<>();
        qw.orderByAsc(Clazz::getId);
        List<Clazz> clazzes = clazzMapper.selectList(qw);
        return RestResponse.success(ClazzConvert.INSTANCE.clazzs2Infos(clazzes));
    }

    @Override
    public RestResponse<PageResponse<ClazzResponse>> queryClasses(ClazzPageRequest classQuery) {
        Page<Clazz> page = new Page<>(classQuery.getPage(), classQuery.getLimit());
        LambdaQueryWrapper<Clazz> qw = new LambdaQueryWrapper<>();
        if (StringUtils.isNotNullOrBlank(classQuery.getName()) && !"-1".equals(classQuery.getName())) {
            qw.like(Clazz::getName, classQuery.getName());
        }
        qw.orderByAsc(Clazz::getId);
        IPage<Clazz> pageData = clazzMapper.selectPage(page, qw);
        List<Clazz> records = pageData.getRecords();
        List<ClazzResponse> list = ClazzConvert.INSTANCE.clazzs2Responses(records);
        return RestResponse.success(new PageResponse<>(list, pageData.getTotal(), classQuery.getPage(), classQuery.getLimit()));
    }

    @Override
    public RestResponse<PageResponse<UserResponse>> queryClassStudents(Long classid) {
        UserPageRequest userPageRequest = new UserPageRequest();
        userPageRequest.setRole(RoleEnum.student.getCode());
        userPageRequest.setClazzId(classid);
        return iUserService.queryUsers(userPageRequest);
    }

    @Override
    public void addStudent(Long classid) {
        Clazz clazz = this.getById(classid);
        clazz.setCount(clazz.getCount() + 1);
        clazz.fillUpdateInfo(httpServletRequest, redisCache);
        this.saveOrUpdate(clazz);
    }

    @Override
    public void removeStudent(Long classid) {
        Clazz clazz = this.getById(classid);
        clazz.setCount(clazz.getCount() - 1);
        clazz.fillUpdateInfo(httpServletRequest, redisCache);
        this.saveOrUpdate(clazz);
    }
}
